package com.syh.jn.scm.service.impl.supplier.base.goods;

import cn.redscarf.commons.util.other.ExtAssert;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.jn.scm.domain.supplier.entity.base.goods.Manufacturer;
import com.syh.jn.scm.domain.supplier.repository.base.goods.ManufacturerRepository;
import com.syh.jn.scm.dto.supplier.base.goods.goodsManufacturer.ManufacturerMto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsManufacturer.ManufacturerSto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsManufacturer.ManufacturerVo;
import com.syh.jn.scm.service.api.supplier.base.goods.ManufacturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产商
 */
@Service
@Transactional
public class ManufacturerServiceImpl extends AbstractServiceImpl<ManufacturerSto, ManufacturerMto, ManufacturerVo, Manufacturer, Long> implements ManufacturerService {
  @Autowired
  private ManufacturerRepository manufacturerRepository;

  @Override
  protected AggregateRepository<Manufacturer, Long> getAggregateRepository() {
    return manufacturerRepository;
  }

  /**
   * 保存对象
   * @param sto
   * @return
   */
  @Override
  public Long save(ManufacturerSto sto) {
    ExtAssert.notNull(sto, "保存对象为空");
    ValidationUtils.validate(sto);

    Manufacturer manufacturer = Manufacturer.createdManufacturerBySupply(sto);
    return manufacturerRepository.add(manufacturer);
  }

  /**
   * 修改对象
   * @param mto
   */
  @Override
  public void update(ManufacturerMto mto) {
    ExtAssert.notNull(mto, "修改对象为空");
    ValidationUtils.validate(mto);
    //根据id获取生产商并且判空
    Manufacturer manufacturer = this.getManufacturerById(mto.getId());

    manufacturer.modify(mto);
    manufacturerRepository.update(manufacturer);
  }

  /**
   * 删除对象
   * @param id
   */
  @Override
  public void delete(Long id) {
    //根据id获取生产商并且判空
    Manufacturer manufacturer = this.getManufacturerById(id);

    manufacturer.destroy();
    manufacturerRepository.removeById(id);
  }

  /**
   * 启用生产商
   * @param id
   */
  @Override
  public void enabled(Long id){
    //根据id获取生产商并且判空
    Manufacturer manufacturer = this.getManufacturerById(id);

    manufacturer.enabled();
    manufacturerRepository.update(manufacturer);
  }

  /**
   * 停用生产商
   * @param id
   */
  @Override
  public void disabled(Long id){
    //根据id获取生产商并且判空
    Manufacturer manufacturer = this.getManufacturerById(id);

    manufacturer.disabled();
    manufacturerRepository.update(manufacturer);
  }

  /**
   * 根据id获取生产商并且判空
   * @param id
   * @return
   */
  public Manufacturer getManufacturerById(Long id){
    ExtAssert.notNull(id, "id为空");

    Manufacturer manufacturer = manufacturerRepository.getById(id);
    ExtAssert.notNull(manufacturer, "对象为空");

    return manufacturer;
  }
}
